export const fetchServer = (url, options = {}) => {
  options = Object.assign(options, {
    credentials: 'include',
    mode: 'cors',
    // mode: 'no-cors',
    headers: {
      'Content-Type': 'application/json'
    },
  })
  if (options.data) {
    options.body = JSON.stringify(options.data);
    delete options.data;
  }
  if (options.query) {
    const query_string_list = [];
    for (let key in options.query) {
      if (options.query[key] !== undefined) {
        query_string_list.push(encodeURIComponent(key) + "=" + encodeURIComponent(options.query[key]));
      }
    }
    url += "?" + query_string_list.join("&")
  }
  //   console.log(options)
  return fetch(server_href + url, options)
    .then(res => res.json())
    .then(res => {
      //   console.log(res)
      if (!res) {
        throw new Error("Response Error: content is null")
      }
      if (res.status === "success") {
        return res.data
      } else {
        throw new Error("Response Error: " + res.msg);
      }
    })
};

export const getSex = (sex_value) => {
  if (sex_value == 1) {
    return "男"
  }
  if (sex_value == 2) {
    return "女"
  }
  return "未知"
};
export const formatDate = (date, format = "lll") => {
  if (date) {
    return moment(date).format(format);
  } else {
    return "未设定的时间"
  }
};
